package com.qqt.csr.route.controller;

import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.idempotent.Idempotent;
import com.qqt.csr.common.interceptor.auth.Authorize;
import com.qqt.csr.common.log.Log;
import com.qqt.csr.common.operalog.BusinessType;
import com.qqt.csr.common.operalog.LogLevel;
import com.qqt.csr.common.operalog.OperationLog;
import com.qqt.csr.common.session.SessionContextHolder;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.common.vo.req.PageRequest;
import com.qqt.csr.common.vo.resp.ObjectResponse;
import com.qqt.csr.common.vo.resp.PageResponse;
import com.qqt.csr.route.entity.ServiceTime;
import com.qqt.csr.route.service.ServiceTimeService;
import com.qqt.csr.route.vo.req.AddingServiceTimeReqVO;
import com.qqt.csr.route.vo.req.DeletingServiceTimeReqVO;
import com.qqt.csr.route.vo.req.QueryingServiceTimeReqVO;
import com.qqt.csr.route.vo.req.UpdatingServiceTimeReqVO;
import com.qqt.csr.route.vo.resp.ServiceTimeInfoRespVO;
import com.qqt.csr.route.vo.resp.ServiceTimeListRespVO;
import com.qqt.csr.workbench.vo.req.DeletingSkillSetReqVO;
import com.qqt.csr.workbench.vo.req.QueryingSkillSetMemberReqVO;
import com.qqt.csr.workbench.vo.req.UpdatingSkillSetReqVO;
import com.qqt.csr.workbench.vo.resp.SkillSetMemberInfoRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Authorize
@RestController
@RequestMapping("csr/service_time")
@Tag(name = "客服服务时间", description = "客服服务时间")
public class CsrServiceTimeController {
    @Autowired
    private ServiceTimeService serviceTimeService;

    @PostMapping("/add")
    @Operation(summary = "添加服务时间组")
    @Log(desc = "添加服务时间组")
    @Idempotent(model = "csr:service_time:add:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "服务时间管理", buttonName = "创建服务时间组", businessType = BusinessType.INSERT)
    public ObjectResponse<String> add(@Validated @RequestBody AddingServiceTimeReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        Integer serviceTime = serviceTimeService.add(tenantId, creatorId, req);
        return ObjectResponse.success(serviceTime.toString());
    }

    @PostMapping("/update")
    @Operation(summary = "修改服务时间组")
    @Log(desc = "修改服务时间组")
    @Idempotent(model = "csr:service_time:update:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "服务时间管理", buttonName = "修改服务时间组", businessType = BusinessType.UPDATE)
    public ObjectResponse<String> update(@Validated @RequestBody UpdatingServiceTimeReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        serviceTimeService.updateBy(tenantId, req);
        return ObjectResponse.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除服务时间组")
    @Log(desc = "删除服务时间组")
    @Idempotent(model = "csr:service_time:delete:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "服务时间管理", buttonName = "删除服务时间组", businessType = BusinessType.DELETE)
    public ObjectResponse<String> delete(@Validated @RequestBody DeletingServiceTimeReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        serviceTimeService.delete(tenantId,req);
        return ObjectResponse.success();
    }

    @PostMapping("/queryPage")
    @Operation(summary = "分页查询服务时间组")
    public PageResponse<ServiceTimeInfoRespVO> queryPage(@Validated @RequestBody PageRequest<QueryingServiceTimeReqVO> req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        if(req.getData() == null){
            req.setData(new QueryingServiceTimeReqVO());
        }
        req.getData().setTenantId(tenantId);
        return serviceTimeService.queryPage(req);
    }

    @GetMapping("/queryList")
    @Operation(summary = "查询服务时间组下拉列表")
    public ObjectResponse<List<ServiceTimeListRespVO>> queryList() {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        List<ServiceTime> serviceTimeList = serviceTimeService.getServiceTimeList(tenantId);
        return ObjectResponse.success(serviceTimeList.stream()
                .map(serviceTime -> ServiceTimeListRespVO.builder().id(serviceTime.getId()).name(serviceTime.getName()).build())
                .toList());
    }
}
